package com.onlinemovies.repository.extension;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Query;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Repository;

import com.onlinemovies.entity.User;
import com.onlinemovies.repository.AbstractRepository;

@Repository("userRepository")
public class UserRepository extends AbstractRepository<User> implements Serializable{

	@SuppressWarnings("unchecked")
	public List<User> getAll() {
	        return entityManager
	        		.createQuery("SELECT u FROM User u ORDER BY u.username")
	        		.getResultList();
	    }

	public boolean doLogIn(User user, HttpSession httpSession) {
		boolean flag=false;
		Query createQuery = entityManager
		.createQuery("SELECT u FROM User u where u.username=?1 and u.password=?2");
		createQuery.setParameter(1, user.getUsername())
		.setParameter(2, user.getPassword());
		List<User> resultList = createQuery.getResultList();
		if(resultList!=null && resultList.size()>0){
			User user2 = resultList.get(0);
			httpSession.setAttribute("user", user2);
			flag=true;
		}
		return flag;
	}
}
